﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinFu.Reflection.Emit;
using Mono.Cecil;

namespace Hiro
{
    /// <summary>
    /// An extension class that adds helper methods to the <see cref="TypeDefinition"/> class.
    /// </summary>
    internal static class TypeDefinitionExtensions
    {
        /// <summary>
        /// Adds a method to a type definition.
        /// </summary>
        /// <param name="containerType">The container type definition.</param>
        /// <param name="methodName">The name of the method.</param>
        /// <param name="methodAttributes">The method attributes.</param>
        /// <param name="parameterTypes">The parameter types that describe the method signature.</param>
        /// <returns>A <see cref="MethodDefinition"/> that represents the new method.</returns>
        public static MethodDefinition DefineMethod(this TypeDefinition containerType, string methodName, MethodAttributes methodAttributes, Type[] parameterTypes)
        {
            var newMethod = containerType.DefineMethod(methodName, methodAttributes, typeof(object), parameterTypes);
            newMethod.Body.InitLocals = true;
            newMethod.ImplAttributes = MethodImplAttributes.IL | MethodImplAttributes.Managed;
            newMethod.HasThis = true;
            return newMethod;
        }
    }
}
